import store from '@/store'
import axios from 'axios'; // 引入axios
import router from '@/router'
const instance = axios.create()
function setDisableZoom(){
  if(store.state.setting.disableZoom){
    import ('@/util/disableZoom')
  }
}


const isBreak = false//中途有没加载成功的情况是否继续加载剩余js
function loadVoLteResourceList (RESOURCE_LIST) {
  return new Promise(r=>{
    RESOURCE_LIST.reduce((res, el) => res[isBreak ? 'then' : 'finally'](() => loadScript(el)).catch(e=>{
      e && console.error(`【加载js内容失败失败】：${e}`)
      return Promise.reject()
    }), Promise.resolve())[isBreak ? 'then' : 'finally'](() => {
      r()
    }).catch(e=>{})
  })
}

function loadScript (url) {
  return new Promise((resolve, reject) => {
    const script = document.createElement('script')

    script.onload = () => resolve()

    script.onerror = () => reject(`Load script from ${url} failed`)

    script.src = url
    const head =
      document.head || document.getElementsByTagName('head')[0]
    ;(document.body || head).appendChild(script)
  })
}
function isLogin(){
  if(store.state.setting.isLogin){
    // router.push('/login');
    // loadVoLteResourceLi st(["http://10.6.1.129/per.js"]).then(()=>{
    //   const systemId = store.state.setting.systemId
    //   select(systemId);
    // })
  }
}
function initClickCount(){
  const {showClickCount,systemId} = store.state.setting
  axios.get("http://10.6.1.129/paih_api/getranking.php",
    {
      params: {
        limit:showClickCount,
        permission:systemId
      }
    }).then(res=>{
    store.commit('SET_CLICK_COUNT_INFOS',res.data)
  });
}
function initNotices(params){
  const  { tabInfos,isMessage }  = store.state.setting.headerTip
  const type1 = {
    type:0,
    date:'2023-01-01',
    message:'月实际消耗比月计划够进相差月实际消耗比月计划够进相差月实际消耗比月计划够进相差月实际消耗比月计划',
    number:'63547',
    path:'/horizontal/demo2'
  }
  const type2 = {
    type:1,
    date:'2023-03-03',
    message:'月实际消耗比月计划够进相差',
    number:'43547',
    path:'/horizontal/demo1'
  }
  const headerTipContent = {}
  if(isMessage){
    headerTipContent.message = [type1, type1, type2]
  }else{
    tabInfos.forEach(info=>(headerTipContent[info.key]=[]))
    headerTipContent.confirmed.push(type1,type1,type1,type2)
    headerTipContent.updated.push(type1,type1,type1,type2,type1)
    headerTipContent.myMessage.push(type1,type1,type1,type2,type2,type2)
  }
  store.commit('SET_HEADER_TIP_CONTENT',headerTipContent)
}
export default {
  install(Vue){
    setDisableZoom()
    isLogin()
    initClickCount()
    initNotices()
    Vue.prototype.$initNotices = initNotices

  }
}
